package top.zhangjianyong.tools.service.mybatis.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.ETFData;
import top.zhangjianyong.tools.mapper.ETFDateMapper;
import top.zhangjianyong.tools.service.mybatis.IETFDataService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author ： zhangjianyong
 * @date ： 2022/9/29 4:27 PM
 */
@Service
public class IETFDateServiceImpl extends ServiceImpl<ETFDateMapper, ETFData> implements IETFDataService {

    @Override
    public ETFData queryOne(String code, LocalDate valueDate) {
        return lambdaQuery()
                .eq(ETFData::getCode, code)
                .eq(ETFData::getValueDate, valueDate)
                .one();
    }

    @Override
    public ETFData lastOne(String code) {

        return lambdaQuery().eq(ETFData::getCode, code)
                .orderByDesc(ETFData::getValueDate)
                .last("limit 1").one();
    }

    @Override
    public ETFData firstOne(String code) {
        return lambdaQuery().eq(ETFData::getCode, code)
                .orderByAsc(ETFData::getValueDate)
                .last("limit 1").one();
    }

    @Override
    public List<ETFData> listOrderByDate(String code) {
        return lambdaQuery().eq(ETFData::getCode, code)
                .orderByAsc(ETFData::getValueDate)
                .list();
    }

    @Override
    public List<Map<String, String>> listEtf() {
        return baseMapper.listEtf();
    }

    @Override
    public List<Map<String, String>> search(String code) {
        return baseMapper.search(code);
    }

    @Override
    public List<ETFData> listAfterDate(String code, LocalDate date) {
        return lambdaQuery().eq(ETFData::getCode, code)
                .ge(ETFData::getValueDate, date)
                .orderByAsc(ETFData::getValueDate)
                .list();
    }

    @Override
    public boolean existByCodeAndDate(String code, LocalDate date) {
        return lambdaQuery().eq(ETFData::getCode, code)
                .eq(ETFData::getValueDate, date)
                .count() > 0;
    }

    @Override
    public List<ETFData> listBetweenDate(String code, LocalDate start, LocalDate end) {
        return lambdaQuery()
                .between(ETFData::getValueDate, start, end)
                .eq(ETFData::getCode, code)
                .orderByAsc(ETFData::getValueDate)
                .list();
    }

    @Override
    public List<ETFData> listByCode(String code) {
        return lambdaQuery().eq(ETFData::getCode, code).orderByAsc(ETFData::getValueDate).list();
    }

    @Override
    public List<ETFData> listLastLimit(String code, long days) {
        return lambdaQuery().eq(ETFData::getCode, code)
                .orderByDesc(ETFData::getValueDate)
                .last(" limit " + days)
                .list();
    }
}
